package com.uenpay.im.ui.old.activity.company;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-2-22 13:02:55
 * @copyRight http://www.qiyunxin.com
 */
public class CompanyAdapter extends BaseAdapter {

	private ArrayList<CompanyEntity> mArrayList = null;
	private LayoutInflater mLayoutInflater = null;
	private Context mContext;
	private boolean choose_company;

	public CompanyAdapter(Context _Context, ArrayList<CompanyEntity> _ArrayList, boolean _choose_company) {
		mLayoutInflater = LayoutInflater.from(_Context);
		mArrayList = _ArrayList;
		this.choose_company = _choose_company;
		mContext = _Context;
	}

	@Override
	public int getCount() {
		return mArrayList == null ? 0 : mArrayList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mArrayList == null ? null : mArrayList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		PhoneBookViewHodler mBookViewHodler = null;
		final CompanyEntity mCompanyEntity = mArrayList.get(arg0);
		if (arg1 == null) {
			arg1 = mLayoutInflater.inflate(R.layout.item_company_layout, arg2, false);
			mBookViewHodler = new PhoneBookViewHodler();
			mBookViewHodler.nameTv = (TextView) arg1.findViewById(R.id.company_name_tv);
			// mBookViewHodler.avatarIv = (RoundImageView)
			// arg1.findViewById(R.id.company_avatar);
			mBookViewHodler.manger_btn = (Button) arg1.findViewById(R.id.manger_btn);
			arg1.setTag(mBookViewHodler);
		} else {
			mBookViewHodler = (PhoneBookViewHodler) arg1.getTag();
		}
		mBookViewHodler.nameTv.setText(mCompanyEntity.entname);
		if (mCompanyEntity._iscreator == 1 && !choose_company) {
			mBookViewHodler.manger_btn.setVisibility(View.VISIBLE);
			// mBookViewHodler.manger_btn.setVisibility(View.GONE);
		} else {
			mBookViewHodler.manger_btn.setVisibility(View.GONE);
		}
		mBookViewHodler.manger_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Bundle bundle = new Bundle();
				bundle.putSerializable("entid", mCompanyEntity.entid);
				bundle.putString("entname", mCompanyEntity.entname);
				ActivityHandle.startActivity((Activity) mContext, ManagerCompanyActivity.class, bundle, -1);

			}
		});
		return arg1;
	}

	private class PhoneBookViewHodler {
		// private RoundImageView avatarIv;
		private TextView nameTv;
		private Button manger_btn;
	}

}
